export DIR="$( cd "$( dirname "$0" )" && pwd )"

export jmeter_exec="$DIR/bin/jmeter.sh"

export sderr="$DIR/../jmeter-stdout.log"
export sderr_backup="$sderr.backup"

export sdout="$DIR/../jmeter-sdout.log"
export sdout_backup="$sdout.backup"

if [ -n "$TEST_OBJECT" ] ; then
   echo "Test object: $TEST_OBJECT"
else
   echo "Test object not exported"
   exit 0
fi

if [ -n "$TESTCASE" ] ; then
   echo "Testcase: $TESTCASE"
else
   echo "TESTCASE not exported"
   exit 0
fi

echo "Surviving TESTCASE..."

export JMETER_LOG_FILE="$DIR/expMiddleware.log"
export JMETER_LOG_BACKUP="$JMETER_LOG_FILE.backup"

export JMETER_OPTS="-n -t $TESTCASE -l $JMETER_LOG_BACKUP"

export THREADS="250 500 750 1000"
#export THREADS="10"

export DELAYS="500 1000 2000 4000 8000 16000"

echo "Surviving variable declarations"

touch $JMETER_LOG_FILE
touch $sderr
touch $sdout

rm $JMETER_LOG_FILE
rm $sderr
rm $sdout

touch $JMETER_LOG_FILE
touch $sderr
touch $sdout

echo "Declaring function..."



for t in $THREADS ; do
   for d in $DELAYS ; do
      
      touch $JMETER_LOG_BACKUP
      touch $sderr_backup
      touch $sdout_backup

      rm $JMETER_LOG_BACKUP
      rm $sderr_backup
      rm $sdout_backup

      touch $JMETER_LOG_BACKUP
      touch $sderr_backup
      touch $sdout_backup

      echo "$jmeter_exec $JMETER_OPTS -Jcamid.testobject=$TEST_OBJECT -Jcamid.threads=$t -Jcamid.delay=$d 2> $sderr_backup > $sdout_backup"
      $jmeter_exec $JMETER_OPTS -Jcamid.testobject=$TEST_OBJECT -Jcamid.threads=$t -Jcamid.delay=$d 2> $sderr_backup > $sdout_backup &

      #pid="$!"

      sleep 13m
      #kill -9 $pid
      killall -s SIGKILL java


      if [ -f "$sderr_backup" ] ; then
         sed "s/TG-Main/TG-$t-$d/" < $sderr_backup >> $sderr
      fi
      if [ -f "$sdout_backup" ] ; then
         sed "s/Downtime/Downtime-TG-$t-$d/" < $sdout_backup >> $sdout
      fi
      if [ -f "$JMETER_LOG_BACKUP" ] ; then
         sed "s/TG-Main/TG-$t-$d/" < $JMETER_LOG_BACKUP >> $JMETER_LOG_FILE
      fi

      rm $JMETER_LOG_BACKUP
      rm $sderr_backup
      rm $sdout_backup

   done
done


echo "Calling function"



